package com.niuke;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class NiuKe1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt(); // 第一个数为缓存一个报告金币数
        String[] fileIds = sc.nextLine().split(" ");
        int[] fileIndexs = new int[fileIds.length]; // 文件标识序列
        for (int i = 0; i < fileIds.length; i++) {
            fileIndexs[i] = Integer.parseInt(fileIds[i]);
        }

        String[] fileSs = sc.nextLine().split(" ");
        int[] fileSizes = new int[fileSs.length]; // 文件大小序列
        for (int i = 0; i < fileSs.length; i++) {
            fileSizes[i] = Integer.parseInt(fileSs[i]);
        }
        int ans = executeAns(m, fileIndexs, fileSizes);
        System.out.println(ans);
    }

    public static int executeAns(int m, int[] fileIndexs, int[] fileSizes) {
        Map<Integer, Integer> counts = new HashMap<>();
        for (int i = 0; i < fileIndexs.length; i++) {
            counts.put(fileIndexs[i], counts.getOrDefault(fileIndexs[i], 0) + 1);
        }

        int res = 0;
        for (int index : counts.keySet()) {
            int count = counts.get(index);
            if (count == 1) { // 只出现一次的只扫描不缓存
                res += fileSizes[index];
            } else { // 其它情况比较扫描一次和缓存一次 与 只扫描的情况，取小值
                int a = fileSizes[index] + m;
                int b = fileSizes[index] * count;
                res += Math.min(a, b);
            }
        }
        return res;
    }

}
